package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.busiexception.BusiException;
import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseEnum;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SystemUserService systemUserService;


    @Override
    public CaptchaVo getCaptchaVo() {
//        图片验证的大小
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
//        验证码的内容
        String verCode = specCaptcha.text().toLowerCase();
//        key 的定义
        String key =RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        stringRedisTemplate.opsForValue().set(key, verCode, RedisConstant.APP_LOGIN_CODE_TTL_SEC, TimeUnit.SECONDS);
//        CaptchaVo 还需要图片信息
        String image = specCaptcha.toBase64();
        // 将key和base64返回给前端
        return new CaptchaVo(image,key);
    }

    @Override
    public String login(LoginVo loginVo) {
//        todo 1 获取参数
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        String captchaKey = loginVo.getCaptchaKey();
        String captchaCode = loginVo.getCaptchaCode();


//        todo 2  判断是否为空
        if(!StringUtils.hasText(username)){
            throw new BusiException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        if(!StringUtils.hasText(password)){
            throw new BusiException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        if(!StringUtils.hasText(captchaKey)){
            throw new BusiException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        if(!StringUtils.hasText(captchaCode)){
            throw new BusiException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        String redisCode = stringRedisTemplate.opsForValue().get(captchaKey);
        if(redisCode == null){
            throw  new BusiException(ResultCodeEnum.TOKEN_EXPIRED);
        }

//        判断验证码内容是否一致 captchaCode
        if( !captchaCode.equalsIgnoreCase(redisCode)){
            throw  new BusiException(ResultCodeEnum.TOKEN_EXPIRED);
        }

//        todo 3 判断是否 一致
        //  判断用户名是否和数据库一致
        SystemUser systemUser = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, username));
        if( systemUser == null){
            throw new BusiException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }

        //   判断密码是否相同  需要给数据库加密  因为前端密码是加密过的
        if(!DigestUtils.md5Hex(password).equals(systemUser.getPassword())){
            throw new BusiException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
//           判断状态是否被禁用是否正确
        if( systemUser.getStatus() == BaseStatus.DISABLE){
            throw new BusiException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
        }

//        判断验证码和redis存储的是否一致

//        todo 4 返回token  创建工具类 用来创建token和解析token
        return JwtUtil.createJwt(systemUser.getId(), systemUser.getUsername());

    }
}
